﻿using System;
using RpcClient;
using RpcHelper;
using RpcModel;
using RpcModularModel.Identity.Msg;
using SqlExecHelper;
using Wedonek.RpcStore.Service.DAL;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.Collect
{

        internal class IdentityAppCollect : BasicCollect<IdentityAppDAL>, IIdentityAppCollect
        {
                public Guid Add (IdentityApp add)
                {
                        return base.BasicDAL.Add (add);
                }
                public void Set (IdentityApp app, IdentitySetParam param)
                {
                        if (param.IsEquals (app))
                        {
                                return;
                        }
                        base.BasicDAL.Set (app.Id, param);
                        app.ConvertInto (param);
                        this._Refresh (app, "set");
                }
                private void _Refresh (IdentityApp app, string type)
                {
                        new RefreshIdentity ()
                        {
                                AppId = app.Id,
                                EventType = type,
                                IsEnable = app.IsEnable
                        }.SyncSend ();
                }
                public IdentityApp[] Query (IdentityQueryParam query, IBasicPage paging, out long count)
                {
                        return base.BasicDAL.Query (query, paging, out count);
                }
                public IdentityApp Get (Guid id)
                {
                        IdentityApp app = base.BasicDAL.Get (id);
                        if (app == null)
                        {
                                throw new ErrorException ("rpc.identity.not.find");
                        }
                        return app;
                }
                public void Delete (IdentityApp app)
                {
                        if (app.IsEnable)
                        {
                                throw new ErrorException ("rpc.identity.already.enable");
                        }
                        IdentityProwerDAL prowerDAL = new IdentityProwerDAL ();
                        using (ITransaction tran = base.GetCollect<ITransaction> ())
                        {
                                base.BasicDAL.Delete (app.Id);
                                prowerDAL.Clear (app.Id);
                                tran.Commit ();
                        }
                        this._Refresh (app, "delete");
                }

                public void SetIsEnable (IdentityApp app, bool isEnable)
                {
                        if (app.IsEnable == isEnable)
                        {
                                return;
                        }
                        base.BasicDAL.SetIsEnable (app.Id, isEnable);
                        app.IsEnable = isEnable;
                        this._Refresh (app, "SetIsEnable");
                }
        }
}
